<template>
  <div>
    <el-form ref="deptForm" :model="deptForm" :rules="rules" label-width="100px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="deptForm.name" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="deptForm.code" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="deptForm.manager" placeholder="请选择部门负责人">
          <el-option v-for="item in managerList" :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="deptForm.introduce" type="textarea" />
      </el-form-item>
      <el-form-item size="large">
        <el-button type="primary" @click="onSubmit">确定</el-button>
        <el-button @click="onCancel">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addDept, getManagerList, getDeptDetail, editDeptDetail } from '@/api/departments'
export default {
  name: 'DeptDialog',
  props: {
    pid: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    // 同级部门的名字不能相同
    const validName = (rule, value, callback) => {
      let exitNameList = this.originList.filter(item => item.pid === this.pid).map(item => item.name)
      if (this.isEdit) {
        // 先用id找到自己,再筛选pid和自己一样的部门,除去自己
        const dept = this.originList.find(item => item.id === this.pid)
        exitNameList = this.originList.filter(item => item.pid === dept.pid && item.id !== this.pid).map(item => item.name)
      }
      exitNameList.includes(value) ? callback(new Error('部门名字不能重复')) : callback()
    }
    // 所有的部门编码不能相同
    const validCode = (rule, value, callback) => {
      let exitCodeList = this.originList.map(item => item.code)
      if (this.isEdit) {
        // 如果是编辑,把自己排除
        exitCodeList = this.originList.filter(item => item.id !== this.pid).map(item => item.code)
      }
      exitCodeList.includes(value) ? callback(new Error('部门编码不能重复')) : callback()
    }
    return {
      deptForm: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请选择部门负责人', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 1, max: 150, message: '长度在 1 到 150 个字符', trigger: 'blur' }
        ]
      },
      managerList: [] // 负责人列表
    }
  },
  created() {
    this.getManagerList()
    this.getDeptDetail()
  },
  methods: {
    // 获取负责人列表
    async getManagerList() {
      const res = await getManagerList()
      this.managerList = res.data
    },

    // 添加子部门
    async addDept() {
      const res = await addDept({ ...this.deptForm, pid: this.pid })
      // console.log(res)
      this.$message.success(res.message)
    },

    // 编辑部门
    async editDeptDetail() {
      const res = await editDeptDetail(this.deptForm)
      console.log(res)
      this.$message.success(res.message)
    },

    // 编辑或新增
    onSubmit() {
      this.$refs.deptForm.validate(async valid => {
        if (!valid) return
        this.isEdit ? this.editDeptDetail() : this.addDept()
        this.$emit('update-Dept')
      })
    },

    // 取消
    onCancel() {
      this.$emit('close')
    },

    // 获取部门详情,回显数据
    async getDeptDetail() {
      // 如果是编辑状态,才要回显数据
      if (this.isEdit) {
        const res = await getDeptDetail(this.pid)
        this.deptForm = res.data
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-select {
    width: 100%;
}

</style>
